package com.woniu.woniu_bx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.woniu_bx.pojo.Goods;
import com.woniu.woniu_bx.pojo.GoodsCategory;
import com.woniu.woniu_bx.pojo.ResponseEntity;
import com.woniu.woniu_bx.pojo.User;
import com.woniu.woniu_bx.service.GoodsCategoryService;
import com.woniu.woniu_bx.service.GoodsService;
import com.woniu.woniu_bx.service.UserService;
import com.woniu.woniu_bx.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : 王泰元
 * @className: SearchController
 * @description: TODO
 * @date : 2022/9/1 10:59
 */
@RestController
public class SearchController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private UserService userService;
    @Autowired
    private GoodsCategoryService goodsCategoryService;

    /**
     * 根据用户搜索内容，查找商品和用户
     *
     * @param searchText 搜索内容
     * @return 有 goodsList 和 userList 两个集合的 hashMap
     * @author: 王泰元
     */
    @GetMapping("/search/{searchText}")
    public ResponseEntity<Map<String, List>> doSearch(@PathVariable String searchText) throws Exception {
        Map<String, List> map = new HashMap<>();

        // 1、查询商品
        //  1.1 先模糊查询类别，得到类别 id 集合
        QueryWrapper<GoodsCategory> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.like("name", searchText);
        List<GoodsCategory> goodsCategoryList = goodsCategoryService.list(categoryQueryWrapper);
        List<Integer> categoryIds = new ArrayList<>();
        for (GoodsCategory goodsCategory : goodsCategoryList) {
            categoryIds.add(goodsCategory.getId());
        }

        //  1.2 模糊查询商品，描述 like 搜索内容 || 类别名称 like 搜索内容 && 状态 = y
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.like("description", searchText)
                .or().in(categoryIds.size() != 0, "category_id", categoryIds)
                .eq("status", "y")
                .orderByDesc("sale_num");
        List<Goods> goodsList = goodsService.list(goodsQueryWrapper);
        for (Goods goods : goodsList) {
            goods.setUser(userService.getById(goods.getUserId()));
            goods.setGoodsCategory(goodsCategoryService.getById(goods.getCategoryId()));
        }
        map.put("goodsList", goodsList);


        // 2、查询用户，模糊查询商品，账户 like 搜索内容 || 昵称 like 搜索内容 || 个性签名 like 搜索内容 && 状态 = y
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("account", searchText)
                .or().like("nick", searchText)
                .or().like("signature", searchText)
                .eq("status", "y")
                .orderByDesc("money");
        List<User> userList = userService.list(userQueryWrapper);
        for (User user : userList) {
            user.setFansCount(userService.getFansCount(user.getId()));
            user.setLikesCount(userService.getLikesCount(user.getId()));
            user.setTotalSaleNums(userService.getTotalSaleNums(user.getId()) == null ? 0 : userService.getTotalSaleNums(user.getId()));
            user.setAge(DateUtil.getAge(user.getBirthday(), "yyyy-MM-dd"));
        }
        map.put("userList", userList);

        return ResponseEntity.SUCCESS("查询成功", map);
    }
}
